/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.addressBook.controller;

import com.addressBook.baseService.Search;
import com.addressBook.bean.Groups;
import com.addressBook.bean.UserInfo;
import com.addressBook.controller.PersonInfoController;
import com.addressBook.tools.CommonPhotoTools;
import com.addressBook.tools.Converter;
import com.addressBook.tools.Parser;
import com.addressBook.util.AllUtil;
import com.fxexperience.javafx.animation.FlipInYTransition;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Random;
import java.util.ResourceBundle;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;

/**
 * FXML Controller class
 *
 * @author Lam
 */
public class UIController implements Initializable {

    public static UIController instance;

    public static UIController getUIController() {
        return instance;
    }
    @FXML
    public Label personInfo, leftButton, rightButton, group;

    @FXML
    public AnchorPane toolPanelContainer;
    public AnchorPane personInfoPanel;

    @FXML
    public AnchorPane mainPane, searchPanel, groupPanel;

    @FXML
    public HBox toolBar, iconBar, personInfoBox1, personInfoBox2, personInfoBox3;

    @FXML
    public ImageView settingIcon, searchIcon;

    @FXML
    private ImageView saveIcon;

    @FXML
    public VBox pageBox;

    int nowpage = 1, page = 0;//nowpage当前页面，page为页面数
    int nowpageInG = 1, pageInG = 0;
    ArrayList<String> colorTable = new ArrayList<String>() {
        {
            add("#008287");
            add("#16499a");
            add("#4390df");
            add("#60a917");
            add("#fa6800");
            add("#9a1616");
        }
    };
    @FXML
    private ImageView clearIcon;

    /**
     * Initializes the controller class.
     */
    @Override
    public void initialize(URL url, ResourceBundle rb) {
        // TODO
        instance = this;
        cleanDate();
        personInfo.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                toolBar.setVisible(false);
                iconBar.setVisible(false);
                pageBox.setVisible(false);
                leftButton.setVisible(false);
                rightButton.setVisible(false);
                personInfoPanel.setOpacity(0);
                toolPanelContainer.setVisible(true);
                personInfoPanel.setVisible(true);
                new FlipInYTransition(personInfoPanel).play();
                PersonInfoController pc = PersonInfoController.getPersonInfoController();
                pc.setText();
            }
        });

        saveIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Parser parser = new Parser();
                parser.saveFile(AllUtil.beanArrayList);
            }
        });

        settingIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                Parser parser = new Parser();
                AllUtil.beanArrayList = parser.openFile();
                ArrayList list = AllUtil.beanArrayList;
                ArrayList<Groups> gList = AllUtil.group;
                AllUtil.personBean = (UserInfo) list.get(0);
                page = list.size() / 8;
                pageInG = gList.size() / 8;
                if (gList.size() % 8 != 0) {
                    pageInG = pageInG + 1;
                }
                if (list.size() % 8 != 0) {
                    page = page + 1;
                }

                personInfoBox1.getChildren().clear();
                personInfoBox2.getChildren().clear();
                personInfoBox3.getChildren().clear();
                paging();
                GroupController gc = GroupController.getGroupController();
                gc.groupBox1.getChildren().clear();
                gc.groupBox2.getChildren().clear();
                gc.groupBox3.getChildren().clear();
                gc.pagingForG(nowpageInG, pageInG);
            }
        });

        searchIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                toolBar.setVisible(false);
                iconBar.setVisible(false);
                pageBox.setVisible(false);
                leftButton.setVisible(false);
                rightButton.setVisible(false);
                searchPanel.setOpacity(0);
                toolPanelContainer.setVisible(true);
                searchPanel.setVisible(true);
                new FlipInYTransition(searchPanel).play();
            }
        });

        clearIcon.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                cleanDate();
            }
        });

        leftButton.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                if (nowpage == 1) {
                    return;
                }
                nowpage = nowpage - 1;
                personInfoBox1.getChildren().clear();
                personInfoBox2.getChildren().clear();
                personInfoBox3.getChildren().clear();
                paging();
            }
        });
        rightButton.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                if (nowpage >= page) {
                    return;
                }
                nowpage = nowpage + 1;
                personInfoBox1.getChildren().clear();
                personInfoBox2.getChildren().clear();
                personInfoBox3.getChildren().clear();
                paging();
            }
        });

//      跳到组界面
        group.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                toolBar.setVisible(false);
                iconBar.setVisible(false);
                pageBox.setVisible(false);
                groupPanel.setOpacity(0);
                toolPanelContainer.setVisible(true);
                groupPanel.setVisible(true);
                GroupController gc = GroupController.getGroupController();
                gc.back.setVisible(true);
                gc.text.setVisible(true);
                gc.pageBox.setVisible(true);
                new FlipInYTransition(groupPanel).play();

//                GroupController gc = GroupController.getGroupController();
            }
        });

    }

    public void back2main() {
        toolPanelContainer.setVisible(false);
        toolBar.setVisible(true);
        iconBar.setVisible(true);
        pageBox.setVisible(true);
        leftButton.setVisible(true);
        rightButton.setVisible(true);
    }

    public void sortPage() {
        ArrayList<UserInfo> list = AllUtil.beanArrayList;
        page = list.size() / 8;
        if (list.size() % 8 != 0) {
            page = page + 1;
        }

        personInfoBox1.getChildren().clear();
        personInfoBox2.getChildren().clear();
        personInfoBox3.getChildren().clear();
        paging();
    }

    public void paging() {
        ArrayList<UserInfo> list = AllUtil.beanArrayList;
//        System.out.println(list.size());
//        System.out.println(page);
//        System.out.println(nowpage);

        int k = (nowpage - 1) * 8;
        int max = nowpage * 8;
        if (page == nowpage) {
            max = list.size();
//            System.out.println(max);
        }
        for (int i = 0; i < 8; i++) {
            UserInfo userBean = list.get(k);
            AnchorPane pane = new AnchorPane();
            pane.setPrefSize(260, 100);
            ImageView personview = new ImageView();
            byte[] buf = CommonPhotoTools.Str2byte(userBean.getPHOTO());
            InputStream is = new ByteArrayInputStream(buf);
            Image image = new Image(is);
            personview.setImage(image);
            personview.setFitHeight(100);
            personview.setFitWidth(100);
            personview.setLayoutX(5);
            personview.setLayoutY(10);
            Label nameLabel = new Label(userBean.getFN());
            nameLabel.setPrefSize(100, 100);
            nameLabel.setLayoutX(120);
            nameLabel.setLayoutY(20);
            nameLabel.setStyle("-fx-text-fill:white;");
            nameLabel.setWrapText(true);
            pane.getChildren().addAll(personview, nameLabel);
            int color = new Random().nextInt(5);
            System.out.println("color" + color);
            pane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
            pane.setOnMouseClicked(new EventHandler<MouseEvent>() {

                @Override
                public void handle(MouseEvent event) {
                    AllUtil.personBean = userBean;
                    toolBar.setVisible(false);
                    iconBar.setVisible(false);
                    pageBox.setVisible(false);
                    leftButton.setVisible(false);
                    rightButton.setVisible(false);
                    personInfoPanel.setOpacity(0);
                    toolPanelContainer.setVisible(true);
                    personInfoPanel.setVisible(true);
                    new FlipInYTransition(personInfoPanel).play();
                    PersonInfoController pc = PersonInfoController.getPersonInfoController();
                    pc.setText();
                }
            });
            if (i <= 2) {
                personInfoBox1.getChildren().add(pane);
                k = k + 1;
            } else if (i <= 5) {
                personInfoBox2.getChildren().add(pane);
                k = k + 1;
            } else if (i <= 7) {
                personInfoBox3.getChildren().add(pane);
                k = k + 1;
            }
            if (k >= max) {
//                System.out.println("------------");
                AnchorPane addPane = new AnchorPane();
                addPane.setPrefSize(260, 100);
                Label addLabel = new Label("新增");
                addLabel.setLayoutX(100);
                addLabel.setLayoutY(80);
                ImageView addImageView = new ImageView("com/addressBook/view/icons/useradd.png");
                addImageView.setFitHeight(80);
                addImageView.setFitWidth(80);
                addImageView.setLayoutX(80);
                addImageView.setLayoutY(0);
                addLabel.setStyle("-fx-text-fill:white;");
                addPane.getChildren().addAll(addLabel, addImageView);
                color = new Random().nextInt(5);
                addPane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
                System.out.println("XINZENG");
                addPane.setOnMouseClicked(new EventHandler<MouseEvent>() {

                    @Override
                    public void handle(MouseEvent event) {
                        AllUtil.personBean = new UserInfo();
                        toolBar.setVisible(false);
                        iconBar.setVisible(false);
                        pageBox.setVisible(false);
                        leftButton.setVisible(false);
                        rightButton.setVisible(false);
                        personInfoPanel.setOpacity(0);
                        toolPanelContainer.setVisible(true);
                        personInfoPanel.setVisible(true);
                        new FlipInYTransition(personInfoPanel).play();
                        PersonInfoController pc = PersonInfoController.getPersonInfoController();
                        pc.setText();
                    }
                });
                if (i < 2) {
//                    System.out.println("111111111");
                    personInfoBox1.getChildren().add(addPane);
                    k = k + 1;
                } else if (i < 5) {
//                    System.out.println("222222222222");
                    personInfoBox2.getChildren().add(addPane);
                    k = k + 1;
                } else if (i < 8) {
                    personInfoBox3.getChildren().add(addPane);
                    k = k + 1;
                }
                break;
            }
        }
    }

    public void cleanDate() {
        page = 0;
        nowpage = 1;
        personInfoBox1.getChildren().clear();
        personInfoBox2.getChildren().clear();
        personInfoBox3.getChildren().clear();
        AllUtil.beanArrayList = null;
        AllUtil.personBean = null;
        AllUtil.group = null;
        AnchorPane addPane = new AnchorPane();
        addPane.setPrefSize(260, 100);
        Label addLabel = new Label("新增");
        addLabel.setLayoutX(100);
        addLabel.setLayoutY(80);
        ImageView addImageView = new ImageView("com/addressBook/view/icons/useradd.png");
        addImageView.setFitHeight(80);
        addImageView.setFitWidth(80);
        addImageView.setLayoutX(80);
        addImageView.setLayoutY(0);
        addLabel.setStyle("-fx-text-fill:white;");
        addPane.getChildren().addAll(addLabel, addImageView);
        int color = new Random().nextInt(5);
        addPane.setStyle("-fx-background-color:" + colorTable.get(color) + ";");
        System.out.println("XINZENG");
        addPane.setOnMouseClicked(new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent event) {
                AllUtil.personBean = new UserInfo();
                toolBar.setVisible(false);
                iconBar.setVisible(false);
                pageBox.setVisible(false);
                leftButton.setVisible(false);
                rightButton.setVisible(false);
                personInfoPanel.setOpacity(0);
                toolPanelContainer.setVisible(true);
                personInfoPanel.setVisible(true);
                new FlipInYTransition(personInfoPanel).play();
                PersonInfoController pc = PersonInfoController.getPersonInfoController();
                pc.setText();
            }
        });
        personInfoBox1.getChildren().add(addPane);
    }
}
